package com.xclink.wechat.service.core.ReplyMessage;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xclink.wechat.service.TokenService;
import com.xclink.wechat.utils.HttpReq;
import com.xclink.wechat.utils.message.MessageUtil;
import com.xclink.wechat.utils.message.resp.TextMessage;

/**
 * 集中处理文本消息之回复文本消息
 * 
 * @author ljp
 *
 */
public class ProcessTextMessage {
	public String process(String toUserName, String fromUserName, String content) {
		TextMessage t = new TextMessage();
		t.setCreateTime(new Date().getTime());
		t.setFromUserName(toUserName);
		t.setToUserName(fromUserName);
		t.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		if (content.equals("百度")) {
			String url = "http://www.baidu.com";
			content = addHref(content, url);
			t.setContent(content);
			return MessageUtil.MessageToXml(t);
		}
		if (content.equalsIgnoreCase("sb")) {
			content = "你是个好人";
			t.setContent(content);
			return MessageUtil.MessageToXml(t);
		}
		if (content.equals("图片")) {
			ProcessImageMessage p= new ProcessImageMessage();
			String mediaId="mfWLzyt4OAooA0QGKINW6-MDFyrLsSNJrkEj-iVXdFJe9BRsg9cgsaAKrk9mS3hc";
			return p.process(toUserName, fromUserName, mediaId);
		}
		if (content.equals("视频")) {
			ProcessVideoMessage p= new ProcessVideoMessage();
			String mediaId="IZHtrccuY8JZ8DriG_jJusWKAnNos0PsQp7tmNCDUB0LgNCx4h1p_Tji9Ac02eoi";
			String thumbMediaId ="MXnUdg9cnjWZdA_Arbx6OgypIFRqhniUHuJitUcVtzV0JDxyEEmFy03xMrvCuGMP";
			return p.process(toUserName, fromUserName, mediaId,thumbMediaId);
		}
		if (content.equals("呵呵")) {
			content = "你很不错";
			t.setContent(content);
			return MessageUtil.MessageToXml(t);
		}
//		转到图文消息
		ProcessNewsMessage p = new ProcessNewsMessage();
		if (p.process(content, toUserName, fromUserName) != null
				&& !"".equals(p.process(content, toUserName, fromUserName))) {
			return p.process(content, toUserName, fromUserName);
		}
		// 转到机器人聊天
		JSONObject json = new JSONObject();
		json.put("key", "690dc818afa9440cb3664680f64a3395");
		// json.put("key", "754cccb4583a4b68b040cbd93bce73f2");
		json.put("info", content);
		json.put("userid", fromUserName);
		String s = HttpReq.sendPost("http://www.tuling123.com/openapi/api", json.toString());
		System.out.println("s====" + s);
		Map textContent = JSON.parseObject(s);
		// 这个code以后做扩展时在用
		Integer code = (Integer) textContent.get("code");
		if (code == 200000) {
			// 发送链接消息
			String url = (String) textContent.get("url");
			t.setContent(url);
			return MessageUtil.MessageToXml(t);
		}
		if (code == 302000) {
			// 发送新闻

		}
		if (code == 308000) {
			// 发送新闻

		}
		if (code == 308000) {
			// 发送诗词（儿童版）

		}
		if (code == 308000) {
			// 发送儿歌（儿童版）

		}
		content = (String) textContent.get("text");
		t.setContent(content);
		return MessageUtil.MessageToXml(t);
	}

	// 给文本加超链接
	public String addHref(String content, String url) {
		return "<a href=" + '"' + url + '"' + ">" + content + "</a>";

	}
}
